#include "business.hpp"

/* 登录模块的实现 以及业务处理 */

void callback(const zhong::Channel::ptr &channel, const std::string &consumer_tag, const zhong::BasicProperties *bp, const std::string &body)
{
    DEBUG_LOG("消费者[%s]接收到消息[%s]", consumer_tag.c_str(), body.c_str());
    channel->basicAck(bp->id());
}
int Entry(int argc, char *argv[])
{
    if (argc != 2)
    {
        ERR_LOG("正确输入为[./business port]");
        return -1;
    }
    return 0;
}
// 发布消息的客户端
int main(int argc, char *argv[])
{
    if (Entry(argc, argv) != 0)
    {
        return -1;
    }
    int port = std::stoi(argv[1]);

    // 1.实例化异步工作线程对象
    zhong::AsyncWorker::ptr worker = std::make_shared<zhong::AsyncWorker>();
    // 2.实例化连接对象
    zhong::Connection::ptr connection = std::make_shared<zhong::Connection>("127.0.0.1", port, worker);
    // 3.通过链接创建并获取信道
    zhong::Channel::ptr channel = connection->openChannel();

    //  4.提供用户登录业务
    // 本质上就是模拟 点击 操作
SERVICE:
    std::cout << "请输入你所需要的服务：[login existed user] or [create new user]" << std::endl;
    std::string service;
    std::getline(std::cin, service);
    //      1.进行登录，获取用户类别
    if (service == "login existed user")
    {
        std::cout << "用户名: ";
        std::string user_name;
        std::getline(std::cin, user_name);
        std::cout << "密码: ";
        std::string password;
        std::getline(std::cin, password);
        // 获取用户
        int level = channel->selectUser(user_name, password);
        // 通过用户level来选择工厂
        if (level == 1)
        {
            zhong::Business::ManagerBusiness(channel);
            connection->closeChannel(channel);
        }
        else if (level == 2)
        {
            zhong::Business::ConsumerBusiness(channel);
            connection->closeChannel(channel);
        }
        else if (level == 3)
        {
            zhong::Business::ProducerBusiness(channel);
            connection->closeChannel(channel);
        }
        else
        {
            ERR_LOG("用户[%s]的level为[%d], 不合法", user_name.c_str(), level);
            return -1;
        }
    }
    //      2.不进行登录，创建新用户
    else if (service == "create new user")
    {
        std::cout << "用户名: ";
        std::string user_name;
        std::getline(std::cin, user_name);
        std::cout << "密码: ";
        std::string password;
        std::getline(std::cin, password);
    CHOICE:
        std::cout << "是否对用户进行持久化存储: [yes] or [no]"<<std::endl;
        bool durable = true;
        std::string choice;
        std::getline(std::cin, choice);
        if (choice == "yes")
        {
            durable = true;
        }
        else if (choice == "no")
        {
            durable = false;
        }
        else
        {
            INFO_LOG("持久化选择有误，返回上一层");
            goto CHOICE;
        }
    LEVEL:
        std::cout << "选择新增的用户身份: [consumer] or [producer]"<<std::endl;
        std::string user;
        std::getline(std::cin, user);
        int level = 0;
        if (user == "consumer")
        {
            level = 2;
        }
        else if (user == "producer")
        {
            level = 3;
        }
        else
        {
            INFO_LOG("用户选择有误，返回上一层");
            goto LEVEL;
        }
        channel->declareUser(user_name, password, durable, level);
        INFO_LOG("用户[%s]创建成功，返回上一层", user_name.c_str());
        goto SERVICE;
    }
    else
    {
        INFO_LOG("服务选择有误，返回上一层");
        goto SERVICE;
    }
}
